(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:LuckydrawCtrl
     * @description
     * # KanjiaCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('LuckydrawCtrl', LuckydrawCtrl);

    LuckydrawCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', 'sessionService', '$interval', '$state'];

    function LuckydrawCtrl($scope, dataService, helper, $rootScope, $location, sessionService, $interval, $state) {

        //抽奖活动列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);
            $scope.$on('event:up-store-id', function() {
                $scope.getList(1);
            });
        }

        $scope.getList = function(pageNo) {
            var query = {
                "content": {
                    store_id: $rootScope.store_id,
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                }
            };

            dataService.luckydrawGet(query).then(function(list) {
                if (list) {
                    for (var i in list.draw_list) {
                        list.draw_list[i].diff_time = $scope.diff_time(list.draw_list[i].start_time, list.draw_list[i].end_time)
                    }
                    $scope.draw_list = list.draw_list;
                    $scope.draw_total = list.total_count;
                }
            });
        }

        //js计算两个时间的时间差
        $scope.diff_time = function(start_time, end_time) {
            var s_time = new Date().getTime();
            var start = new Date(start_time).getTime();
            var end = new Date(end_time).getTime();

            //当前时间是否大于活动结束时间
            if (s_time > end) {
                return 0;
            }

            //判断当前时间是否大于开始时间
            if (s_time > start && s_time < end) {
                var time = new Date().getTime();
            } else {
                var time = new Date(start_time).getTime();
            }

            var diff = end - time;

            //计算出相差天数
            var days = Math.floor(diff / (24 * 3600 * 1000));

            //计算出小时数  
            var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数  
            var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
            var minutes = Math.floor(leave2 / (60 * 1000));

            //计算相差秒数  
            var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
            var seconds = Math.round(leave3 / 1000);

            var returnStr = seconds + "秒";
            if (minutes > 0) {
                returnStr = minutes + "分" + returnStr;
            }
            if (hours > 0) {
                returnStr = hours + "小时" + returnStr;
            }
            if (days > 0) {
                returnStr = days + "天" + returnStr;
            }
            return returnStr;
        }

        //抽奖活动添加
        $scope.addAction = function() {
            var draw_id = $location.search().draw_id;

            if (draw_id) {
                var query = { "content": { "draw_id": draw_id, "store_id": $rootScope.store_id } };

                dataService.luckydrawGet(query).then(function(result) {
                    if (result) {
                        $scope.draw_data = result.draw_list[0];
                        $scope.draw_data.introduce = json2html(JSON.parse($scope.draw_data.introduce));
                        if ($scope.draw_data.introduce1) {
                            $scope.draw_data.introduce1 = json2html(JSON.parse($scope.draw_data.introduce1));
                        }
                        if ($scope.draw_data.introduce2) {
                            $scope.draw_data.introduce2 = json2html(JSON.parse($scope.draw_data.introduce2));
                        }
                        $scope.draw_data.store_id = $rootScope.store_id;

                        $scope.draw_id = result.draw_id;
                    }
                });
            } else {
                var data = {
                    "store_id": $rootScope.store_id,
                    "name": "猜数字赢大奖",
                    "img": "",
                    "guess_number": 367,
                    "lottery_number": 10,
                    "start_time": $scope.getDateStr(0),
                    "end_time": $scope.getDateStr(7),
                    "introduce": "猜数字赢大奖",
                    "introduce1": "猜数字赢大奖",
                    "introduce2": "猜数字赢大奖",
                    "remark": "备注"
                };
                $scope.draw_data = data;
            }
        }

        // 抽奖添加
        $scope.drawAdd = function() {
            if ($scope.draw_data.end_time < $scope.draw_data.start_time) {
                helper.myAlert("error:活动结束时间不能小于活动开始时间");
                return false;
            }

            if ($scope.draw_data.lottery_number <= 0) {
                helper.myAlert("error:抽奖次数设置错误");
                return false;
            }

            var draw_id = $location.search().draw_id;
            if (draw_id) {
                dataService.luckydrawUpdate($scope.draw_data).then(function(result) {
                    if (result.Msg == "ok" && result.draw_id) {
                        helper.myAlert("success", "#/luckydraw/list");
                    } else {
                        helper.myAlert("error：" + result.Msg);
                        return false;
                    }
                });
            } else {
                dataService.luckydrawAdd($scope.draw_data).then(function(result) {
                    if (result.Msg == "ok" && result.draw_id) {
                        $scope.draw_id = result.draw_id;
                        helper.myAlert("success", "#/luckydraw/list");
                    } else {
                        helper.myAlert("error：" + result.Msg);
                        return false;
                    }
                });
            }
        }

        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            var s = dd.getSeconds();

            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }

        //详情数据
        // var time_code;
        $scope.detailAction = function() {
            var draw_id = $scope.draw_id = $location.search().draw_id;
            if (draw_id) {
                $scope.activity_number(draw_id);
                $scope.pull_number(draw_id);
                $scope.pull_people(draw_id);

                //隔几秒刷新活动人数以及头像列表
                /*time_code = $interval(function() {
                    $scope.activity_number(draw_id);
                }, 5000, time_code);*/

            } else {
                helper.myAlert("error：没有抽奖活动！");
                return false;
            }
        }

        //活动参与人数，头像列表
        $scope.activity_number = function(draw_id) {
            dataService.drawdetailGet(1000, draw_id).then(function(result) {
                if (result.detail_count) {
                    //竞猜人数
                    $scope.activity_number = result.detail_count;
                    // 头像，角标数据
                    $scope.detail_list = result.detail_list;
                } else {
                    $scope.activity_number = 0;

                    helper.myAlert("error：没有用户参与活动！");
                    return false;
                }
            });
        }

        //总的拉人数，新人榜
        $scope.pull_number = function(draw_id) {
            var query = { "content": { "draw_id": draw_id } };
            dataService.drawpullGet(query).then(function(result) {
                if (result.pull_count) {
                    //拉人数
                    $scope.pull_number = result.pull_count;
                    //榜单
                    $scope.new_list = result.pull_list;
                } else {
                    $scope.pull_number = 0;

                    // helper.myAlert("error：没有用户拉取好友！");
                    return false;
                }
            });
        }

        //拉人排行榜
        $scope.pull_people = function(draw_id) {
            if (draw_id) {
                dataService.drawdetailGet(10, draw_id).then(function(result) {
                    if (result.detail_count) {
                        $scope.pull_people = result.detail_list;
                    } else {
                        // helper.myAlert("error：没有新人加入！");
                        return false;
                    }
                });
            }
        }

        //中奖人排行榜
        $scope.getWinList = function() {
            if (!$scope.draw_id) {
                helper.myAlert("error：活动编号不能为空！");
                return false;
            }

            if (!$scope.win_number) {
                helper.myAlert("error：中奖数字不能为空！");
                return false;
            }

            dataService.winPeopleGet($scope.draw_id, $scope.win_number).then(function(result) {
                if (result.winList) {
                    $scope.winList = result.winList;
                }
            });
        }

        //设置某个用户竞猜的某个数字中奖
        $scope.setWin = function(status, guess_id) {
            if (status) {
                var is_win = 1;
            } else {
                var is_win = 0;
            }
            var data = { 'is_win': is_win, "guess_id": guess_id };

            dataService.setWin(data).then(function(result) {
                if (result.Msg == "ok") {
                    helper.myAlert("success");
                    $scope.getWinList();
                } else {
                    helper.myAlert("error:" + result.Msg);
                    return false;
                }
            });
        }

        //伪造数据
        $scope.fakedataAction = function() {
            var data = {
                "act_num": 1
            };

            $scope.fake_data = data;
        }

        //保存活动人数
        $scope.saveActNum = function() {
            if ($scope.fake_data.draw_id) {
                if ($scope.fake_data.act_num) {
                    dataService.drawdetailAdd($scope.fake_data.draw_id, $scope.fake_data.act_num).then(function(result) {
                        if (result.Msg == "ok") {
                            helper.myAlert("success", null, null, function() {
                                $state.go("luckydraw.list");
                            });
                        } else {
                            helper.myAlert("error：参与活动人数不能为空");
                            return false;
                        }
                    });
                } else {
                    helper.myAlert("error：参与活动人数不能为空");
                    return false;
                }
            } else {
                helper.myAlert("error：没有选择抽奖活动，不能进行操作！");
                return false;
            }
        }

        $scope.actinfo = function(actInfo) {
            console.log("actInfo--->", actInfo);
            if (actInfo) {
                $scope.actInfo = actInfo;
                $scope.fake_data.draw_id = actInfo.draw_id;
            }
        }
    }
})();